﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectClone.Tool
{
    public class FileHelper
    {
        /// <summary>
        /// 拷贝sourcePath的文件到destinationPath下面
        /// </summary>
        /// <param name="sourcePath">lab文件所在目录</param>
        /// <param name="destinationPath">保存的目标目录</param>
        /// <returns>返回:true-拷贝成功;false:拷贝失败</returns>
        public static bool CopyOldFilesToNewFile(string sourcePath, string destinationPath)
        {
            if (!Directory.Exists(destinationPath))
            {
                Directory.CreateDirectory(destinationPath);
            }

            #region 拷贝labs文件夹到savePath下
            try
            {
                string[] labDirs = Directory.GetDirectories(sourcePath);//目录
                string[] labFiles = Directory.GetFiles(sourcePath);//文件
                if (labFiles.Length > 0)
                {
                    for (int i = 0; i < labFiles.Length; i++)
                    {
                        File.Copy(sourcePath + "\\" + Path.GetFileName(labFiles[i]), destinationPath + "\\" + Path.GetFileName(labFiles[i]), true);
                    }
                }
                if (labDirs.Length > 0)
                {
                    for (int j = 0; j < labDirs.Length; j++)
                    {
                        Directory.GetDirectories(sourcePath + "\\" + Path.GetFileName(labDirs[j]));

                        //递归调用
                        CopyOldFilesToNewFile(sourcePath + "\\" + Path.GetFileName(labDirs[j]), destinationPath + "\\" + Path.GetFileName(labDirs[j]));
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
            #endregion
            return true;
        }

        /// <summary>
        /// p2相对于p1的相对路径
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        public static string MakeRelativePath(string p1, string p2)
        {
            if (string.IsNullOrWhiteSpace(p1) || string.IsNullOrWhiteSpace(p2)) {
                return string.Empty;
            }

            if (!p1.EndsWith("\\"))
            {
                p1 = p1 + "\\";
            }

            Uri u1 = new Uri(p1, UriKind.Absolute);
            Uri u2 = new Uri(p2, UriKind.Absolute);
            Uri u3 = u1.MakeRelativeUri(u2);//u2相对于u1的uri
            return u3.OriginalString.Replace("/","\\");
        }

        /// <summary>
        /// 根据p1的绝对路径和p2相对于p1的相对路径获取p2的绝对路径
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        public static string MakeAbsolutePath(string p1, string p2)
        {
            if (string.IsNullOrWhiteSpace(p1) || string.IsNullOrWhiteSpace(p2))
            {
                return string.Empty;
            }

            if (!p1.EndsWith("\\"))
            {
                p1 = p1 + "\\";
            }

            Uri u1 = new Uri(p1, UriKind.Absolute);
            Uri u2 = new Uri(p2, UriKind.Relative);
            Uri u3 = new Uri(u1, u2);
            return u3.LocalPath.Replace("/", "\\");
        }
    }
}
